<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  .weui-cells__title {
    text-align: left;
  }
  .oitem {
    padding: 3px;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label {
    text-align: left;
  }
  .time {
    border: none;
    font-size: 17px;
  }
  .weui-select {
    height: auto;
    line-height: auto;
  }
</style>
<template>
  <div class="addsell">
    <div style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      录入销售订单
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="../../assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form action="/api/sellform/add" method="post" id="formid">
        <input type="hidden" name="depguid" :value="defaultDep.value" />
        <input type="hidden" name="customerguid" :value="customerguid" />
        <input type="hidden" name="storermguid" />
        <input type="hidden" name="deliprovince" />
        <input type="hidden" name="delicity" />
        <input type="hidden" name="delidistrict" />
        <input type="hidden" name="arriprovince" />
        <input type="hidden" name="arricity" />
        <input type="hidden" name="arridistrict" />
        <input type="hidden" name="salestype" :value="defSaletype.value" />
        <input type="hidden" name="faretype" :value="defFaretype.value" />
        <input type="hidden" name="farepricetype" :value="defFpricetype.value" />
        <div class="cellgroup">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label" ref="msgDiv">申请人</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input notnull" type="text" name="applyname" readonly="true" :value="username" style="width: 35%;">
              <input class="weui-input notnull" id="depname" type="text" name="depname" :value="defaultDep.title" placeholder="请选择部门" style="width: 61%;">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">客户</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input notnull" id="customername" type="text" :value="customername" name="customername" placeholder="请选择客户">
            </div>
          </div>
          <div class="weui-cell" v-if="hasCustomer">
            <div class="weui-cell__hd"><label class="weui-label">应收账款</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input notnull" id="rcamount" type="text" name="rcamount" :value="rcamount">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">销售形式</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" id="salestype" type="text" name="" :value="defSaletype.title" placeholder="请选择销售形式">
            </div>
          </div>
        </div>
<!--        <div class="cellgroup">-->
          <!--        <div class="weui-cell">-->
          <!--          <div class="weui-cell__hd"><label class="weui-label">发货工厂</label></div>-->
          <!--          <div class="weui-cell__bd">-->
          <!--            <input class="weui-input" type="text" id="storermname" name="" placeholder="请选择库房">-->
          <!--          </div>-->
          <!--        </div>-->
          <!--        <div class="weui-cell">-->
          <!--          <div class="weui-cell__hd"><label class="weui-label">发货城市</label></div>-->
          <!--          <div class="weui-cell__bd">-->
          <!--            <input class="weui-input" type="text" id="startCity" name="" placeholder="请选择发货城市" >-->
          <!--          </div>-->
          <!--        </div>-->
          <!--        <div class="weui-cell">-->
          <!--          <div class="weui-cell__hd"><label class="weui-label">详细地址</label></div>-->
          <!--          <div class="weui-cell__bd">-->
          <!--            <input class="weui-input notnull" type="text" id="deliaddress" name="deliaddress" placeholder="请输入发货详细地址">-->
          <!--          </div>-->
          <!--        </div>-->
          <!--        <div class="weui-cell">-->
          <!--          <div class="weui-cell__hd"><label class="weui-label">发货联系人</label></div>-->
          <!--          <div class="weui-cell__bd">-->
          <!--            <input class="weui-input" type="text" id="delicontact" name="delicontact" placeholder="请输入发货联系人">-->
          <!--          </div>-->
          <!--        </div>-->
          <!--        <div class="weui-cell">-->
          <!--          <div class="weui-cell__hd"><label class="weui-label">联系电话</label></div>-->
          <!--          <div class="weui-cell__bd">-->
          <!--            <input class="weui-input" type="text" id="delicontacttel" name="delicontacttel" placeholder="请输入发货人电话">-->
          <!--          </div>-->
          <!--        </div>-->
<!--        </div>-->
        <div class="cellgroup">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">收货城市</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="endCity" name="" placeholder="请选择收货城市" >
            </div>
            <div class="weui-cell__ft" v-if="hasCustomer">
              <div class="weui-vcode-btn" @click="manageAddr"><i class="weui-icon-info-circle"></i></div>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">详细地址</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="arriaddres" name="arriaddres" placeholder="请输入收货详细地址">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">收货联系人</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="arricontact" name="arricontact" placeholder="请输入收货联系人">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">联系电话</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="arricontactno" name="arricontactno" placeholder="请输入收货人电话">
            </div>
          </div>
        </div>
        <div class="cellgroup">
          <div class="weui-cell fir">
            <div class="weui-cell__hd"><label class="weui-label">物流结算</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="faretype" name="" :value="defFaretype.title" placeholder="请选择物流结算方式">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">预计运费</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="logprice" name="logprice" placeholder="预计运费" style="width: 35%;">
              <input class="weui-input notnull" id="farepricetype" type="text" name="" :value="defFpricetype.title" placeholder="请选择运费计价方式" style="width: 61%;">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd">
              <label class="weui-label">要求发货日期</label>
            </div>
            <div class="weui-cell__bd">
              <input type="text" id='datetime-picker' class="time" name="senddate" placeholder="请选择要求发货日期" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd">
              <label class="weui-label">要求到货日期</label>
            </div>
            <div class="weui-cell__bd">
              <input type="text" id='datetime-picker2' class="time" name="arrivedate" placeholder="请选择要求到货日期" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">结算方式</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="settletype">
                <option value="1">全款预付</option>
                <option value="2">上车付款</option>
                <option value="3">到货付款</option>
                <option value="4">账期支付</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">结算账期</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="settlebilling">
                <option value="1">到货后</option>
                <option value="2">收到发票后</option>
                <option value="3">发货前</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">收到发票后结算天数</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" name="settledays" placeholder="请输入收到发票后结算天数">
            </div>
          </div>
<!--          <div class="weui-cell" >-->
<!--            <div class="weui-cell__hd"><label class="weui-label">是否压车</label></div>-->
<!--            <div class="weui-cell__bd">-->
<!--                <textarea class="weui-textarea" name="unloaddesc" id="unloaddesc" placeholder="请输压车内容" rows="2"></textarea>-->
<!--            </div>-->
<!--          </div>-->
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">备注</label></div>
            <div class="weui-cell__bd">
                <textarea class="weui-textarea" name="remarks" id="" placeholder="请输备注内容" rows="2"></textarea>
            </div>
          </div>
        </div>
        <div class="oitem">
          <div class="weui-cells__title">货品明细一
            <p id="acts" style="float: right;color: #1aad19;">
              <span id="addItem" style="padding-right: 5px;">添加</span>
            </p>
          </div>
          <div class="weui-cell fir">
            <div class="weui-cell__hd"><label for="productguid1" class="weui-label">品种</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="productguid1" name="" placeholder="请选择品种">
              <input class="product" type="hidden" id="pguid1" name="pitems[0].productguid">
              <input type="hidden" id="pname1" name="pitems[0].productname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label for="spguid1" class="weui-label">规格</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="spguid1" name="" placeholder="请选择规格">
              <input class="specif" type="hidden" id="sguid1" name="pitems[0].spguid">
              <input type="hidden" id="sname1" name="pitems[0].spname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">单价(元/吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="price1" name="pitems[0].price" placeholder="请输入单价">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">订单重量(吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="weight1" name="pitems[0].weight" placeholder="请输入订单重量">
            </div>
          </div>
          <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
              <div class="weui-cell__bd">
                <textarea class="weui-textarea" name="pitems[0].remark" id="remark1" placeholder="请输备注内容" rows="3"></textarea>
              </div>
            </div>
          </div>
        </div>
        <div class="oitem hide">
          <div class="weui-cells__title">货品明细二</div>
          <div class="weui-cell fir">
            <div class="weui-cell__hd"><label for="productguid2" class="weui-label">品种</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="productguid2" name="" placeholder="请选择品种">
              <input class="product" type="hidden" disabled="true" id="pguid2" name="pitems[1].productguid">
              <input type="hidden" disabled="true" id="pname2" name="pitems[1].productname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label for="spguid2" class="weui-label">规格</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="spguid2" name="" placeholder="请选择规格">
              <input class="specif" type="hidden" disabled="true" id="sguid2" name="pitems[1].spguid">
              <input type="hidden" disabled="true" id="sname2" name="pitems[1].spname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">单价(元/吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="price2" name="pitems[1].price" disabled="true" placeholder="请输入单价">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">订单重量(吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="weight2" name="pitems[1].weight" disabled="true" placeholder="请输入订单重量">
            </div>
          </div>
          <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
              <div class="weui-cell__bd">
                <textarea class="weui-textarea" name="pitems[1].remark" id="remark2" disabled="true" placeholder="请输备注内容" rows="3"></textarea>
              </div>
            </div>
          </div>
        </div>
        <div class="oitem hide">
          <div class="weui-cells__title">货品明细三</div>
          <div class="weui-cell fir">
            <div class="weui-cell__hd"><label for="productguid3" class="weui-label">品种</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="productguid3" name="" placeholder="请选择品种">
              <input class="product" type="hidden" disabled="true" id="pguid3" name="pitems[2].productguid">
              <input type="hidden" disabled="true" id="pname3" name="pitems[2].productname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label for="spguid3" class="weui-label">规格</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="spguid3" name="" placeholder="请选择规格">
              <input class="specif" type="hidden" disabled="true" id="sguid3" name="pitems[2].spguid">
              <input type="hidden" disabled="true" id="sname3" name="pitems[2].spname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">单价(元/吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="price3" name="pitems[2].price" disabled="true" placeholder="请输入单价">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">订单重量(吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="weight3" name="pitems[2].weight" disabled="true" placeholder="请输入订单重量">
            </div>
          </div>
          <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
              <div class="weui-cell__bd">
                <textarea class="weui-textarea" name="pitems[2].remark" id="remark3" disabled="true" placeholder="请输备注内容" rows="3"></textarea>
              </div>
            </div>
          </div>
        </div>
        <div class="oitem hide">
          <div class="weui-cells__title">货品明细四</div>
          <div class="weui-cell fir">
            <div class="weui-cell__hd"><label for="productguid4" class="weui-label">品种</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="productguid4" name="" placeholder="请选择品种">
              <input class="product" type="hidden" disabled="true" id="pguid4" name="pitems[3].productguid">
              <input type="hidden" disabled="true" id="pname4" name="pitems[3].productname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label for="spguid4" class="weui-label">规格</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="spguid4" name="" placeholder="请选择规格">
              <input class="specif" type="hidden" disabled="true" id="sguid4" name="pitems[3].spguid">
              <input type="hidden" disabled="true" id="sname4" name="pitems[3].spname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">单价(元/吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="price4" name="pitems[3].price" disabled="true" placeholder="请输入单价">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">订单重量(吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="weight4" name="pitems[3].weight" disabled="true" placeholder="请输入订单重量">
            </div>
          </div>
          <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
              <div class="weui-cell__bd">
                <textarea class="weui-textarea" name="pitems[3].remark" id="remark4" disabled="true" placeholder="请输备注内容" rows="3"></textarea>
              </div>
            </div>
          </div>
        </div>
        <div class="oitem hide">
          <div class="weui-cells__title">货品明细五</div>
          <div class="weui-cell fir">
            <div class="weui-cell__hd"><label for="productguid5" class="weui-label">品种</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="productguid5" name="" placeholder="请选择品种">
              <input class="product" type="hidden" disabled="true" id="pguid5" name="pitems[4].productguid">
              <input type="hidden" disabled="true" id="pname5" name="pitems[4].productname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label for="spguid5" class="weui-label">规格</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="spguid5" name="" placeholder="请选择规格">
              <input class="specif" type="hidden" disabled="true" id="sguid5" name="pitems[4].spguid">
              <input type="hidden" disabled="true" id="sname5" name="pitems[4].spname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">单价(元/吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="price5" name="pitems[4].price" disabled="true" placeholder="请输入单价">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">订单重量(吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" id="weight5" name="pitems[4].weight" disabled="true" placeholder="请输入订单重量">
            </div>
          </div>
          <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
              <div class="weui-cell__bd">
                <textarea class="weui-textarea" name="pitems[4].remark" id="remark5" disabled="true" placeholder="请输备注内容" rows="3"></textarea>
              </div>
            </div>
          </div>
        </div>

        <div class="weui-btn-area">
<!--          <a class="weui-btn weui-btn_primary halfbtn" href="javascript:" @click="postForm(1)">保存</a>-->
          <a class="weui-btn weui-btn_primary subbtn" href="javascript:" @click="postForm(2)">提交</a>
        </div>
<!--            <div class="weui-btn-area">-->
<!--              <a class="weui-btn weui-btn_primary" @click="toConfirmFare" >运费确认测试，用完即删</a>-->
<!--              <a class="weui-btn weui-btn_primary" @click="toOutStore" >出库测试，用完即删</a>-->
<!--              <a class="weui-btn weui-btn_primary" @click="toSendCar" >派车测试，用完即删</a>-->
<!--            </div>-->
      </form>
    </div>
  </div>
</template>

<script>
import OutStoreRoom from "./OutStoreRoom";

export default {
  name: 'AddSellform',
  data () {
    return {
      openid: localStorage.openid,//'oIuV2v77MaLCAQILhjI6h6oTWpHI',
      username: localStorage.username,
      customers: [],
      departments: [],
      defaultDep: {},
      fpricetypes: [],
      defFpricetype: {},
      salestypes: [],
      defSaletype: {},
      faretypes: [],
      defFaretype: {},
      customerguid: '',
      customername: '',
      rcamount: '',
      storeCitys: {},
      storeAddrs: {},
      storeRnames: {},
      storeTels: {},
      customerCitys: {},
      customerAddrs: {},
      customerRnames: {},
      customerTels: {},
      itemRemarks: {},
      hasCustomer: false,
      cusrcamounts: {},
      saleSpec:[]
    }
  },
  created() {
    window.addEventListener('setItem', ()=> {
      this.openid = localStorage.getItem('openid');
      this.username = localStorage.getItem('username');
    })
  },
  mounted () {
    //日历初始化
    $("#datetime-picker").calendar();
    $("#datetime-picker2").calendar();

    //添加明细item
    $('form').on('click', '#addItem', function () {
      $('.oitem:hidden').eq(0).find('input').attr("disabled",false);
      $('.oitem:hidden').eq(0).find('textarea').attr("disabled",false);
      $('.oitem:hidden').eq(0).find('select').attr("disabled",false);
      $('.oitem:hidden').eq(0).show();
      updateActBtns();
    })
    //删除明细
    $('form').on('click', '#delItem', function () {
      var elm = $(this).parents('.oitem');
      $.confirm("确定要删除该货品明细吗？", function () {
        elm.find('input').val('');
        elm.find('textarea').val('');
        elm.hide();
        $('.oitem:hidden').eq(0).find('input').attr("disabled",true);
        $('.oitem:hidden').eq(0).find('textarea').attr("disabled",true);
        updateActBtns();
      })
    })
    //调整添加、删除按钮位置
    function updateActBtns() {
      $('#acts').remove();
      var actHtml = '<p id="acts" style="float: right;color: #1aad19;"><span id="addItem" style="padding-right: 5px;">添加</span><span>|</span><span id="delItem" style="padding-left: 5px;">删除</span></p>';

      if ($('.oitem:visible').length == 5) {
        actHtml = '<p id="acts" style="float: right;color: #1aad19;"><span id="delItem" style="padding-left: 5px;">删除</span></p>';
      } else if ($('.oitem:visible').length == 1) {
        var actHtml = '<p id="acts" style="float: right;color: #1aad19;"><span id="addItem" style="padding-right: 5px;">添加</span></p>';
      }
      $('.oitem:visible:last').find('.weui-cells__title').append(actHtml);
    }
    // $("#startCity").cityPicker({
    //   title: "请选择发货地址",
    //   onChange: function (data) {
    //     let addr = data.displayValue;
    //     $('input[name="deliprovince"]').val(addr[0]);
    //     $('input[name="delicity"]').val(addr[1]);
    //     $('input[name="delidistrict"]').val(addr[2]);
    //   }
    // });
    // $('#startCity').change(function(){
    // })
    // $("#endCity").cityPicker({
    //   title: "请选择收货地址",
    //   onChange: function (data) {
    //     let addr = data.displayValue;
    //     $('input[name="arriprovince"]').val(addr[0]);
    //     $('input[name="arricity"]').val(addr[1]);
    //     $('input[name="arridistrict"]').val(addr[2]);
    //   }
    // });

    //获取select下拉数据
    //this.getCustomer();
    this.getDepartment();
    this.getStorms();
    this.getProductinfo();
    this.getSalesspec();
    this.getFpricetype();
    this.getSalestype();
    this.getFaretype();

  },
  activated() {
    //从state中获取数据并赋值
    this.getStateAddr();
    this.setCustomer();
  },
  watch: {
    'departments': function (data) {
      this.defaultDep = data[0];
    },
    'fpricetypes': function (data) {
      this.defFpricetype = data[1];
    },
    'salestypes': function (data) {
      this.defSaletype = data[0];
    },
    'faretypes': function (data) {
      this.defFaretype = data[0];
    }
  },
  methods: {
    setCustomer: function() {
      let customer = this.$store.state.customer;
      this.customerguid = customer.guid;
      this.customername = customer.name;
      this.rcamount = customer.rcamount;
      this.hasCustomer = customer.guid != '' ? true : false;
    },
    manageAddr: function() {
      this.$router.push({name: 'AddressList'});
      return false;
    },
    getStateAddr: function() {
      console.log('last update:', this.$store.state.arriAddr)
      let addr = this.$store.state.arriAddr;
      $('input[name="arriprovince"]').val(addr.province);
      $('input[name="arricity"]').val(addr.city);
      $('input[name="arridistrict"]').val(addr.district);
      $('#endCity').val(addr.province + ' ' + addr.city + ' ' + addr.district);
      $('#arriaddres').val(addr.address);
      $('#arricontact').val(addr.contact);
      $('#arricontactno').val(addr.contactno);
    },
    toOutStore: function() {
      this.$router.push({name: 'OutStoreRoom', query: {guid: 'D998DC13BFF34217A3EFC8DB41526D24'}})
    },
    toSendCar: function() {
      location.href = '/vuecode/#/sellform/sendCar?openid=oIuV2v77MaLCAQILhjI6h6oTWpHI&guid=72BE5664AB5D47D68DF9B8E6128BADD5';
      // this.$router.push({name: 'SendCar', params: {guid: '72BE5664AB5D47D68DF9B8E6128BADD5'}})
    },
    toConfirmFare: function() {
      this.$router.push({name: 'ConfirmFare', query: {guid: '72BE5664AB5D47D68DF9B8E6128BADD5'}})
    },
    //客户
    filterCustomer: function(item) {
      let sname = item.name;
      this.customerCitys[sname] = item.province + ' ' + item.city + ' ' + item.district;
      this.customerAddrs[sname] = item.address;
      this.customerRnames[sname] = item.contact;
      this.customerTels[sname] = item.contactno;
      this.cusrcamounts[sname] = item.rcamount != '' ? item.rcamount : 0;
      return {title: item.name, value: item.guid };
    },
    getCustomer: function(departd) {
      let that = this;
      this.axios({
        method: 'get',
        responseType: 'json',
        url: '/sales/Sellform!findCustomers.action?departd=' + departd
      }).then(response => {
        if (response.data.status == "true") {
          that.customers = response.data.result.map(that.filterCustomer);
          $('#customername').select({
            title: "选择客户",
            items: that.customers,
            onChange: function (data) {
              let sn = data.titles;
              if (sn != undefined) {
                let customer = {
                  guid: data.values,
                  name: data.titles,
                  rcamount: that.cusrcamounts[sn]
                };
                console.log('customer:', customer)
                that.$store.commit('setCustomer', customer);
                that.customerguid = data.values;
                that.customername = data.titles;
                //显示应收账款
                that.hasCustomer = true;
                that.rcamount = that.cusrcamounts[sn] + ' 元';
                console.log('citys:', sn)
                let citys = that.customerCitys[sn].split(' ');
                let storeAddr = {
                  province: citys[0],
                  city: citys[1],
                  district: citys[2],
                  address: that.customerAddrs[sn],
                  contact: that.customerRnames[sn],
                  contactno: that.customerTels[sn]
                };
                that.$store.commit('setArriAddr', storeAddr);

                $('#endCity').val(that.customerCitys[sn]);
                $('#arriaddres').val(that.customerAddrs[sn]);
                $('#arricontact').val(that.customerRnames[sn]);
                $('#arricontactno').val(that.customerTels[sn]);
                let cits2 = that.customerCitys[sn];
                if (cits2 != undefined) {
                  let citys = cits2.split(' ');
                  $('input[name="arriprovince"]').val(citys[0]);
                  $('input[name="arricity"]').val(citys[1]);
                  $('input[name="arridistrict"]').val(citys[2]);
                }
              }

            }
          });
        } else {
          this.toastText('获取客户数据失败');
        }
      });
    },
    //部门
    filterDepartment: function(item) {
      return {title: item.name, value: item.guid };
    },
    getDepartment: function() {
      let that = this;
      this.axios({
        method: 'get',
        responseType: 'json',
        url: '/sales/Sellform!findDepartment.action?openid=' + this.openid,
        params: {sPermission: 2}
      }).then(response => {
        if (response.data.status == "true") {
          that.departments = response.data.result.map(that.filterDepartment);
          if(that.departments.length != 0){
              /**
               * 2020-3-19
               * 根据部门获取客户信息
               * liukx
               */
              that.getCustomer(that.departments[0].value)
          }


          $('#depname').select({
            title: "选择部门",
            items: that.departments,
            onChange: function (data) {
              $('input[name="depguid"]').val($('#depname').data('values'));
                /**
                 * 2020-3-19
                 * 根据部门获取客户信息
                 * liukx
                 */
                that.getCustomer($('#depname').data('values'))
            }
          });
        } else {
          this.toastText('获取部门数据失败');
        }
      });
    },
    //运费计价方式
    filterFpricetype: function(item) {
      return {title: item.dicttext, value: item.dictkey };
    },
    getFpricetype: function() {
      let that = this;
      this.axios({
        method: 'get',
        responseType: 'json',
        url: '/sales/Sellform!findDict.action',
        params: {type: 20}
      }).then(response => {
        if (response.statusText == "OK") {
          that.fpricetypes = response.data.map(that.filterFpricetype);
          $('#farepricetype').select({
            title: "选择计价方式",
            items: that.fpricetypes,
            onChange: function (data) {
              $('input[name="farepricetype"]').val($('#farepricetype').data('values'));
            }
          });
        } else {
          this.toastText('获取运费计价方式数据失败');
        }
      });
    },
    //销售形式
    getSalestype: function() {
      let that = this;
      this.axios({
        method: 'get',
        responseType: 'json',
        url: '/sales/Sellform!findDict.action',
        params: {type: 13}
      }).then(response => {
        if (response.statusText == "OK") {
          that.salestypes = response.data.map(that.filterFpricetype);
          $('#salestype').select({
            title: "选择销售形式",
            items: that.salestypes,
            onChange: function (data) {
              $('input[name="salestype"]').val($('#salestype').data('values'));
            }
          });
        } else {
          this.toastText('获取销售形式数据失败');
        }
      });
    },
    //物流结算
    getFaretype: function() {
      let that = this;
      this.axios({
        method: 'get',
        responseType: 'json',
        url: '/sales/Sellform!findDict.action',
        params: {type: 19}
      }).then(response => {
        if (response.statusText == "OK") {
          that.faretypes = response.data.map(that.filterFpricetype);
          $('#faretype').select({
            title: "选择物流结算方式",
            items: that.faretypes,
            onChange: function (data) {
              $('input[name="faretype"]').val($('#faretype').data('values'));
            }
          });
        } else {
          this.toastText('获取物流结算方式数据失败');
        }
      });
    },
    //库房
    filterStorms: function(item) {
      let sname = item.name;
      this.storeCitys[sname] = item.province + ' ' + item.city + ' ' + item.area;
      this.storeAddrs[sname] = item.address;
      this.storeRnames[sname] = item.contact;
      this.storeTels[sname] = item.contacttel;
      return {title: item.name, value: item.guid };
    },
    getStorms: function() {
      let that = this;
      this.axios({
        method: 'get',
        responseType: 'json',
        url: '/sales/Sellform!findStorms.action?openid=' + this.openid
      }).then(response => {
        if (response.data.status == "true") {
          let storms = response.data.result.map(that.filterStorms);
          $('#storermname').select({
            title: "选择库房",
            items: storms,
            onChange: function (data) {
              let sn = $('#storermname').val();
              $('input[name="storermguid"]').val($('#storermname').data('values'));
              $('#startCity').val(that.storeCitys[sn]);
              $('#deliaddress').val(that.storeAddrs[sn]);
              $('#delicontact').val(that.storeRnames[sn]);
              $('#delicontacttel').val(that.storeTels[sn]);
              let cits = that.storeCitys[sn];
              if (cits != undefined) {
                let citys = cits.split(' ');
                $('input[name="deliprovince"]').val(citys[0]);
                $('input[name="delicity"]').val(citys[1]);
                $('input[name="delidistrict"]').val(citys[2]);
              }
            }
          });
        } else {
          $.toast('获取库房数据失败', 'text')
        }
      });
    },
    //品种
    filterProductinfo: function(item) {
      return {title: item.productname, value: item.guid };
    },
    getProductinfo: function() {
      let that = this;
      this.axios({
        method: 'get',
        responseType: 'json',
        url: '/sales/Sellform!findProductinfo.action?openid=' + this.openid
      }).then(response => {
        if (response.data.status == "true") {
          let productinfo = response.data.result.map(that.filterProductinfo);
          //产品明细
          $('#productguid1').select({
            title: "选择产品",
            items: productinfo,
            onChange: function (data) {
              $('#pguid1').val($('#productguid1').data('values'));
              $('#pname1').val($('#productguid1').val());
                that.addSpaseOption(1);
            }
          });
          $('#productguid2').select({
            title: "选择产品",
            items: productinfo,
            onChange: function (data) {
              $('#pguid2').val($('#productguid2').data('values'));
              $('#pname2').val($('#productguid2').val());
                that.addSpaseOption(2);
            }
          });
          $('#productguid3').select({
            title: "选择产品",
            items: productinfo,
            onChange: function (data) {
              $('#pguid3').val($('#productguid3').data('values'));
              $('#pname3').val($('#productguid3').val());
                that.addSpaseOption(3);
            }
          });
          $('#productguid4').select({
            title: "选择产品",
            items: productinfo,
            onChange: function (data) {
              $('#pguid4').val($('#productguid4').data('values'));
              $('#pname4').val($('#productguid4').val());
                that.addSpaseOption(4);
            }
          });
          $('#productguid5').select({
            title: "选择产品",
            items: productinfo,
            onChange: function (data) {
              $('#pguid5').val($('#productguid5').data('values'));
              $('#pname5').val($('#productguid5').val());
                that.addSpaseOption(5);
            }
          });
        } else {
          $.toast('获取货品数据失败', 'text')
        }
      });
    },
    //规格
    filterSalesspec: function(item) {
      let sname = item.name;
      this.itemRemarks[sname] = item.description;
      return {title: item.name, value: item.guid };
    },
      /**
       * 2020-01-13
       * 1.规格表增加产品ID，与产品关联
       * 2.销售订单提交、生产入库申请单提交、库存调整申请等于规格选择相关的业务，规格下拉列表根据产品进行过滤，仅展示该产品相关的规格
       * @author liukx
       * @param index
       */
    addSpaseOption:function (index){
        var that = this;
        var salesSpecs =  [];
        $('#spguid' + index).val('');
        $.each(that.saleSpec, function (i, item) {
            if($('#productguid' + index).data('values') == item.productGuid){
                var option = {"title":item.name,"value":item.guid};
                salesSpecs.push(option);
            }
        });
        console.log();
        if(salesSpecs.length == 0 && $('#productguid' + index).data('values') != undefined){
            $('#spguid' + index).attr('placeholder','无可选规格');
            $('#spguid' + index).attr('disabled','disabled');

            return;
        }else{
            $('#spguid' + index).attr('placeholder','请选择规格');
            $('#spguid' + index).removeAttr("disabled");
        }
        console.log("111",salesSpecs)
        $('#spguid' + index).select("update",{
            title: "选择规格",
            items: salesSpecs,
            onChange: function (data) {
                $('#sguid' + index).val($('#spguid' + index).data('values'));
                $('#sname' + index).val($('#spguid' + index).val());
                $('#remark' + index).val(that.itemRemarks[$('#spguid' + index).val()]);
            }
        });
    },
    getSalesspec: function() {
      let that = this;
      this.axios({
        method: 'get',
        responseType: 'json',
        url: '/sales/Sellform!findSalesspecification.action?openid=' + this.openid
      }).then(response => {
        if (response.data.status == "true") {


          let salesspec = response.data.result.map(that.filterSalesspec);
            that.saleSpec = response.data.result;
          //产品明细

          $('#spguid2').select({
            title: "选择规格",
            items: salesspec,
            onChange: function (data) {
              $('#sguid2').val($('#spguid2').data('values'));
              $('#sname2').val($('#spguid2').val());
              let sn = $('#spguid2').val();
              $('#remark2').val(that.itemRemarks[sn]);
            }
          });
          $('#spguid3').select({
            title: "选择规格",
            items: salesspec,
            onChange: function (data) {
              $('#sguid3').val($('#spguid3').data('values'));
              $('#sname3').val($('#spguid3').val());
              let sn = $('#spguid3').val();
              $('#remark3').val(that.itemRemarks[sn]);
            }
          });
          $('#spguid4').select({
            title: "选择规格",
            items: salesspec,
            onChange: function (data) {
              $('#sguid4').val($('#spguid4').data('values'));
              $('#sname4').val($('#spguid4').val());
              let sn = $('#spguid4').val();
              $('#remark4').val(that.itemRemarks[sn]);
            }
          });
          $('#spguid5').select({
            title: "选择规格",
            items: salesspec,
            onChange: function (data) {
              $('#sguid5').val($('#spguid5').data('values'));
              $('#sname5').val($('#spguid5').val());
              let sn = $('#spguid5').val();
              $('#remark5').val(that.itemRemarks[sn]);
            }
          });
        } else {
          $.toast('获取规格数据失败', 'text')
        }
      });
    },
    //提交表单数据
    postForm: function(type) {
      if ($('input[name="customerguid"]').val() == '') {
        $.alert('请选择客户！');
        return false;
      } else if ($('input[name="salestype"]').val() == '') {
        $.alert('请选择销售形式！');
        return false;
      } else if ($('input[name="arricity"]').val() == '') {
        $.alert('请选择收货城市！');
        return false;
      } else if ($('input[name="faretype"]').val() == '') {
        $.alert('请选择物流结算！');
        return false;
      } else if ($('input[name="logprice"]').val() == '') {
        $.alert('请输入预计运费！');
        return false;
      } else if ($('#pguid1').val() == '') {
        $.alert('请选择货品品种！');
        return false;
      } else if ($('#sguid1').val() == '') {
        $.alert('请选择货品规格！');
        return false;
      } else if ($('#price1').val() == '') {
        $.alert('请输入货品单价！');
        return false;
      } else if ($('#weight1').val() == '') {
        $.alert('请选择货品重量！');
        return false;
      } else if (!this.checkProduct()) {
        $.toast('货品明细产品重复！')
        return false;
      }
      //防止重复提交
      $('.subbtn').attr("disabled",true).css("pointer-events","none");

      this.axios({
        method: 'post',
        url: '/sales/Sellform!saveOrUpdate.action?openid=' + this.openid + '&opertype=' + type,
        params: this.serialize2Obj($("#formid").serializeArray())
      }).then(response => {
        //恢复提交按钮可点击
        $('.subbtn').attr('disabled', false).css("pointer-events","auto");

        if (response.data.msg == 'success') {
          if (type == 1) {
            this.toastSuccess("保存成功");
          } else {
            this.toastSuccess("提交成功");
            this.$router.push({name: 'Sellform'});
          }
        } else {
          if (response.data.msg == "outlogin") {
            this.toastError('请重新登录');
          } else if (response.data.msg == "canotopt") {
            this.toastError('不允许的操作');
          } else {
            this.toastError('系统错误');
          }
        }
      }).catch(function (error) {
        //恢复提交按钮可点击
        $('.subbtn').attr('disabled', false).css("pointer-events","auto");
        this.toastError('系统错误');
      });
    },
    //判断明细产品是否重复
    checkProduct: function () {
      let parr1 = [];
      let parr2 = [];
      for (let i=0; i<$('.oitem').not(':hidden').length; i++) {
        parr1.push({[$('.product').eq(i).val()]: $('.specif').eq(i).val()});
        parr2.push({[$('.product').eq(i).val()]: $('.specif').eq(i).val()});
      }
      //json数组去重
      for (var i = 0; i < parr2.length; i++) {
        for (var j =i+1; j <parr2.length; ) {
          if (Object.keys(parr2[i])[0] == Object.keys(parr2[j])[0] && Object.values(parr2[i])[0] == Object.values(parr2[j])[0] ) {
            parr2.splice(j, 1);//去除重复的对象；
          }else {
            j++;
          }
        }
      }
      return parr1.length == parr2.length ? true : false;
    }
  }
}
</script>
